﻿using MediatR;
using RBAC.Domain.WarehouseManagement;
using RBAC.ErrorCode;
using RBAC.Interstructrue.Base;
using RBAC.Write.Api.Application.Command.ERP.WarehouseManagement;

namespace RBAC.Write.Api.Application.Handler.ERP.WarehouseManagement
{
    public class UpdateWarehouseListCommandHandler : IRequestHandler<UpdateWarehouseListCommands, APIResult<int>>
    {
        private readonly IBaseRepository<WarehouseList> warehouseListRep;

        public UpdateWarehouseListCommandHandler(IBaseRepository<WarehouseList> warehouseListRep)
        {
            this.warehouseListRep = warehouseListRep;
        }

        public async Task<APIResult<int>> Handle(UpdateWarehouseListCommands request, CancellationToken cancellationToken)
        {
            try
            {
                // 参数验证
                if (request.warehouseList == null)
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库信息不能为空",
                        Data = 0
                    };
                }

                // 验证必填字段
                if (request.warehouseList.Id <= 0)
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库ID不能为空",
                        Data = 0
                    };
                }

                if (string.IsNullOrWhiteSpace(request.warehouseList.WarehouseCode))
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库代码不能为空",
                        Data = 0
                    };
                }

                if (string.IsNullOrWhiteSpace(request.warehouseList.WarehouseName))
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库名称不能为空",
                        Data = 0
                    };
                }

                // 检查仓库是否存在
                var existingWarehouse = warehouseListRep.GetModel(request.warehouseList.Id);
                if (existingWarehouse == null)
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库不存在，无法修改",
                        Data = 0
                    };
                }

                // 检查仓库代码是否与其他仓库重复（排除当前仓库）
                var duplicateWarehouse = warehouseListRep.GetFirstOrDefault(
                    "WarehouseCode = @WarehouseCode AND Id != @Id", 
                    new { WarehouseCode = request.warehouseList.WarehouseCode, Id = request.warehouseList.Id }
                );
                if (duplicateWarehouse != null)
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库代码已存在，请使用其他代码",
                        Data = 0
                    };
                }

                // 保留原有的创建信息
                request.warehouseList.CreateTime = existingWarehouse.CreateTime;
                request.warehouseList.CreateUser = existingWarehouse.CreateUser;
                request.warehouseList.IsDeleted = existingWarehouse.IsDeleted;

                // 使用Dapper进行修改操作
                var result = warehouseListRep.Update(request.warehouseList);

                if (result > 0)
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Success,
                        Msg = "仓库修改成功",
                        Data = result
                    };
                }
                else
                {
                    return new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "仓库修改失败",
                        Data = 0
                    };
                }
            }
            catch (Exception ex)
            {
                return new APIResult<int>
                {
                    Code = APIEnums.Fail,
                    Msg = "修改仓库时发生错误：" + ex.Message,
                    Data = 0
                };
            }
        }
    }
}
